package com.itjcloud.iot.gateway.config;

import com.itjcloud.iot.gateway.model.Result;
import com.itjcloud.iot.gateway.model.ServiceRequestException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.springframework.cloud.gateway.support.TimeoutException;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.reactive.resource.NoResourceFoundException;

import java.net.ConnectException;

/**
 * @author 孟繁森
 */
@RestControllerAdvice
@Slf4j
@Component
public class ItjCloudGatewayErrorConfigure {

    /**
     * 处理没有权限异常
     *
     * @param e 没有权限
     * @return 响应信息
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public Result<String> handler(AccessDeniedException e) {
        log.error("没有权限-AccessDeniedException", e);
        return Result.other(HttpStatus.FORBIDDEN.value(), null, "没有权限，请联系管理员授权");
    }


    /**
     * 全局异常处理
     *
     * @param e 异常
     * @return 处理
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result<String> handler(Exception e) {
        log.error("全局异常", e);
        return Result.other(HttpStatus.INTERNAL_SERVER_ERROR.value(), null, "请求超时, 请联系管理员");
    }


    @ExceptionHandler(NotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public Result<String> handler(NotFoundException e) {
        log.error("未找到该资源{}", e.getMessage(), e);
        return Result.other(HttpStatus.NOT_FOUND.value(), null, "未找到该资源");
    }

    @ExceptionHandler(NoResourceFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public Result<String> handler(NoResourceFoundException e) {
        log.error("未找到该资源{}", e.getMessage(), e);
        return Result.other(HttpStatus.NOT_FOUND.value(), null, "未找到该资源");
    }

    @ExceptionHandler({TimeoutException.class})
    @ResponseStatus(HttpStatus.GATEWAY_TIMEOUT)
    public Result<String> handler(TimeoutException e) {
        log.error("服务访问超时{}", e.getMessage(), e);
        return Result.other(HttpStatus.GATEWAY_TIMEOUT.value(), null, "请求超时");
    }

    @ExceptionHandler({ConnectException.class})
    @ResponseStatus(HttpStatus.GATEWAY_TIMEOUT)
    public Result<String> handler(ConnectException e) {
        log.error("目标服务拒绝连接{}", e.getMessage(), e);
        return Result.other(HttpStatus.GATEWAY_TIMEOUT.value(), null, "目标服务拒绝连接");
    }

    @ExceptionHandler({IllegalArgumentException.class})
    @ResponseStatus(HttpStatus.FOUND)
    public Result<String> handler(IllegalArgumentException e) {
        log.error("参数不能为空{}", e.getMessage(), e);
        return Result.other(HttpStatus.FOUND.value(), null, e.getMessage());
    }

    @ExceptionHandler({ServiceRequestException.class})
    @ResponseStatus(HttpStatus.FOUND)
    public Result<String> handler(ServiceRequestException e) {
        log.error("调用其他服务请求失败,服务报错code{},报错信息{},提示用户信息是{}", e.getCode(), e.getMsg(), e.getUserTip(), e);
        return Result.other(HttpStatus.FOUND.value(), null, e.getUserTip());
    }


}
